﻿Namespace DoFactory.GangOfFour.Strategy.RealWorld2

    Public Interface IBehaviour
        Function moveCommand() As Integer
    End Interface

    Public Class AgressiveBehaviour
        Implements IBehaviour

        Public Function moveCommand() As Integer Implements IBehaviour.moveCommand
            Console.WriteLine("\tAgressive Behaviour: if find another robot attack it")
            Return 1
        End Function
    End Class

    Public Class DefensiveBehaviour
        Implements IBehaviour

        Public Function moveCommand() As Integer Implements IBehaviour.moveCommand
            Console.WriteLine("\tDefensive Behaviour: if find another robot run from it")
            Return -1
        End Function

    End Class

    Public Class NormalBehaviour
        Implements IBehaviour

        Public Function moveCommand() As Integer Implements IBehaviour.moveCommand
            Console.WriteLine("\tNormal Behaviour: if find another robot ignore it")
            Return 0
        End Function

    End Class


    Public Class Robot

        Private behaviour As IBehaviour
        Private name As String

        Public Sub New(ByVal _name As String)
            Me.name = _name
        End Sub

        Public Sub setBehaviour(ByVal behaviour As IBehaviour)
            Me.behaviour = behaviour
        End Sub

        Public Function getBehaviour() As IBehaviour
            Return behaviour
        End Function

        Public Sub move()
            Console.WriteLine(Me.name & ": Based on current position the behaviour object decide the next move:")
            Dim command As Integer = Me.behaviour.moveCommand()
            Console.WriteLine("\tThe result returned by behaviour object " & "is sent to the movement mechanisms " & " for the robot '" + Me.name + "'")
        End Sub

        Public Function GetName() As String
            Return Me.name
        End Function

        Public Sub SetName(ByVal name As String)
            Me.name = name
        End Sub

    End Class

    Public Class Main
        Public Shared Sub main()
            Dim r1 As Robot = New Robot("Big Robot")
            Dim r2 As Robot = New Robot("George v.2.1")
            Dim r3 As Robot = New Robot("R2")

            r1.setBehaviour(New AgressiveBehaviour())
            r2.setBehaviour(New DefensiveBehaviour())
            r3.setBehaviour(New NormalBehaviour())

            r1.move()
            r2.move()
            r3.move()

            Console.WriteLine("\r\nNew behaviours: " & _
              "\r\n\t'Big Robot' gets really scared" & _
              "\r\n\t, 'George v.2.1' becomes really mad because" & _
              "it's always attacked by other robots" & _
              "\r\n\t and R2 keeps its calm\r\n")

            r1.setBehaviour(New DefensiveBehaviour())
            r2.setBehaviour(New AgressiveBehaviour())

            r1.move()
            r2.move()
            r3.move()
        End Sub

    End Class


End Namespace